package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Expenditure;
import com.hqyj.dao.ExpenditureMapper;
import com.hqyj.pojo.Income;
import com.hqyj.service.IExpenditureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 支出表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class ExpenditureServiceImpl  implements IExpenditureService {
    @Autowired
    ExpenditureMapper expenditureMapper;
    //加载分页
    @Override
    public HashMap<String, Object> select(Expenditure expenditure) {
        System.err.println("A");
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        //创建分页对象
        Page<Expenditure> page = new Page<>(expenditure.getPage(),expenditure.getRow());

        //定义条件构造器
        QueryWrapper<Expenditure> queryWrapper = new QueryWrapper<>();
        //查询数据库
        Page<Expenditure> rowsPageList = expenditureMapper.selectPage(page, queryWrapper);
        //设置时间
        List<Expenditure> records = rowsPageList.getRecords();
        for (Expenditure record : records) { ;
            record.setExtime(new SimpleDateFormat("yyyy-MM-dd").format(record.getExpenditureTime()));
        }
//        System.err.println("B");
        //定义当前页集合
        map.put("list",rowsPageList.getRecords());
        //总页数
        map.put("totalPage",rowsPageList.getPages());
        //总条数
        map.put("total",rowsPageList.getTotal());
        //当前页
        map.put("curPage",expenditure.getPage());
        //如果当前页为1，则上一页为也为1
        if (rowsPageList.getCurrent() == 1){
            //上一页
            map.put("prePage",1);
        }else {
            map.put("prePage",expenditure.getPage()-1);
        }
        //如果当前页为最后一页，则下一页为也为最大页
        if (expenditure.getPage() == rowsPageList.getPages()){
            //下一页
            map.put("nextPage",rowsPageList.getPages());
        }else {
            map.put("nextPage",expenditure.getPage()+1);
        }
        //显示每页条数
        map.put("row",expenditure.getRow());
        System.err.println("C");
        return map;
    }

    @Override
    public HashMap<String, Object> del(int expenditureId) {
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        int num = expenditureMapper.deleteById(expenditureId);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> queryList(Expenditure expenditure) {
        //创建分页对象
        Page<Expenditure> page = new Page<>(expenditure.getPage(),expenditure.getRow());
        HashMap<String,Object> map = new HashMap<>();
        //查询数据库
        Page<Expenditure> expenditurePage=expenditureMapper.query(page);

        //当前页集合
        map.put("list",expenditurePage.getRecords());
        //总页数
        map.put("totalPage",expenditurePage.getPages());
        //总条数
        map.put("total",expenditurePage.getTotal());
        //当前页
        map.put("curPage",expenditure.getPage());
        if(expenditure.getPage()<=1){
            //上一页
            if (expenditure.getPage()==0){
                map.put("prePage",0);
            }else {
                map.put("prePage",1);
            }
        }else{
            map.put("prePage",expenditure.getPage()-1);

        }
        if(expenditure.getPage()>=expenditurePage.getPages()){
            //下一页
            map.put("nextPage",expenditurePage.getPages());

        }else{
            map.put("nextPage",expenditure.getPage()+1);

        }
        //每页显示条数
        map.put("row",expenditure.getRow());
        return map;
    }
}
